﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emu8080.Parser.GeneratedParser;
using Emu8080.Parser.GeneratedLexer;
using Antlr.Runtime;
using System.IO;
using Antlr.Runtime.Tree;

namespace Emu8080.Parser
{

    public enum AsmTokenType : int
    {
        T_LABEL			= AsmI8080Parser.T_LABEL,
        T_COMMENT		= AsmI8080Parser.T_COMMENT,
        T_OP_0			= AsmI8080Parser.T_OP_0,
        T_OP_IM16		= AsmI8080Parser.T_OP_IM16,
        T_OP_RP_IM16	= AsmI8080Parser.T_OP_RP_IM16,
        T_OP_IM8		= AsmI8080Parser.T_OP_IM8,
        T_OP_REG_IM8	= AsmI8080Parser.T_OP_REG_IM8,
        T_OP_REG_REG	= AsmI8080Parser.T_OP_REG_REG,
        T_OP_REG		= AsmI8080Parser.T_OP_REG,
        T_OP_RP			= AsmI8080Parser.T_OP_RP,
        T_REF_LABEL		= AsmI8080Parser.T_REF_LABEL,
        T_INT			= AsmI8080Parser.T_INT,
        T_CMD			= AsmI8080Parser.T_CMD
    }

    public static class AsmParser
    {
        public static CommonTree Parse(TextReader tr)
        {
            var input = new ANTLRReaderStream(tr);
            var lex = new AsmI8080Lexer(input);
            var tokens = new CommonTokenStream(lex);
            var parser = new AsmI8080Parser(tokens);
            parser.TreeAdaptor = new CommonTreeAdaptor();
            var ret = parser.parse();
            return ret.Tree as CommonTree;
        }

        public static CommonTree ParseText(string text)
        {
            return Parse(new StringReader(text));
        }

        public static CommonTree ParseFile(string path)
        {
            return Parse(new StreamReader(path));
        }
    }
}
